﻿
using System;
using System.Collections.Generic;
using System.Threading;

namespace AsyncEvtEcho
{
    static class Program
    {
        private static void RunAsServer(int port)
        {
            using (EchoServer server = new EchoServer(handler => new NewEvtArgsPool(handler)))
            {
                server.Run(port);
                Console.ReadLine();
            }// using
        }

        private static IEnumerable<string> DelayMessages(IEnumerable<string> messages, int interval)
        {
            foreach (var message in messages)
            {
                Thread.Sleep(interval);
                yield return message;
            }
        }

        private static void RunAsClient(string address, int port)
        {
            string[] messsages = { "Hello WSU", "Test SocketAsyncEventArgs", "Father's Day", "Tianjin Explosion", "Stasi", "Cheka" };

            using (EchoClient client = new EchoClient(Utility.CmdToUpper))
            {
                client.Run(address, port, DelayMessages(messsages, 1000));
                Console.ReadLine();
            }// using
        }

        public static void Main(string[] args)
        {
            string ipAddress = "127.0.0.1";
            int port = 7027;

            if (args.Length != 1)
                throw new ArgumentException("only one argument needed");
            else if (args[0].Equals("server", StringComparison.OrdinalIgnoreCase))
                RunAsServer(port);
            else if (args[0].Equals("client", StringComparison.OrdinalIgnoreCase))
                RunAsClient(ipAddress, port);
            else
                throw new ArgumentException("un-recognized option");
        }
    }
}
